<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li class="active"><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 异步回调">1. 异步回调</a><ul><li><a href="#t11.1 回调地狱">1.1 回调地狱</a></li><li><a href="#t21.2 并行结果">1.2 并行结果</a></li></ul></li><li><a href="#t32. Promise">2. Promise</a></li><li><a href="#t43. Promise的三种状态">3. Promise的三种状态</a></li><li><a href="#t54. 构造一个Promise">4. 构造一个Promise</a><ul><li><a href="#t64.1  使用Promise">4.1  使用Promise</a></li><li><a href="#t74.2  es5模拟Promise">4.2  es5模拟Promise</a></li><li><a href="#t84.3  es6模拟Promise">4.3  es6模拟Promise</a></li></ul></li><li><a href="#t95. promise 做为函数的返回值">5. promise 做为函数的返回值</a></li><li><a href="#t106.promise的链式调用">6.promise的链式调用</a></li><li><a href="#t117.promise API">7.promise API</a><ul><li><a href="#t127.1 Promise.all">7.1 Promise.all</a></li><li><a href="#t137.2 Promise.race">7.2 Promise.race</a></li><li><a href="#t147.3 Promise.resolve">7.3 Promise.resolve</a></li><li><a href="#t157.4 Promise.reject">7.4 Promise.reject</a></li></ul></li><li><a href="#t168. q">8. q</a><ul><li><a href="#t178.1 q的基本用法">8.1 q的基本用法</a></li><li><a href="#t188.2 q的简单实现">8.2 q的简单实现</a></li><li><a href="#t198.3 q的实现">8.3 q的实现</a></li></ul></li><li><a href="#t209. bluebird">9. bluebird</a><ul><li><a href="#t219.1  bluebird经典使用">9.1  bluebird经典使用</a></li><li><a href="#t229.2  bluebird简单实现">9.2  bluebird简单实现</a></li></ul></li><li><a href="#t2310. 动画">10. 动画</a></li><li><a href="#t2411. co">11. co</a><ul><li><a href="#t2511.1  co初体验">11.1  co初体验</a></li><li><a href="#t2611.2  co连续读文件">11.2  co连续读文件</a></li></ul></li><li><a href="#t2712. Promise/A+完整实现">12. Promise/A+完整实现</a></li><li><a href="#t2813. 资源">13. 资源</a></li><li><a href="#t2914.作业">14.作业</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x5F02;&#x6B65;&#x56DE;&#x8C03;">1. &#x5F02;&#x6B65;&#x56DE;&#x8C03; <a href="#t01. &#x5F02;&#x6B65;&#x56DE;&#x8C03;"> # </a></h2>
<h3 id="t11.1 &#x56DE;&#x8C03;&#x5730;&#x72F1;">1.1 &#x56DE;&#x8C03;&#x5730;&#x72F1; <a href="#t11.1 &#x56DE;&#x8C03;&#x5730;&#x72F1;"> # </a></h3>
<p>&#x5728;&#x9700;&#x8981;&#x591A;&#x4E2A;&#x64CD;&#x4F5C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x5BFC;&#x81F4;&#x591A;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x5D4C;&#x5957;&#xFF0C;&#x5BFC;&#x81F4;&#x4EE3;&#x7801;&#x4E0D;&#x591F;&#x76F4;&#x89C2;&#xFF0C;&#x5C31;&#x662F;&#x5E38;&#x8BF4;&#x7684;&#x56DE;&#x8C03;&#x5730;&#x72F1;</p>
<h3 id="t21.2 &#x5E76;&#x884C;&#x7ED3;&#x679C;">1.2 &#x5E76;&#x884C;&#x7ED3;&#x679C; <a href="#t21.2 &#x5E76;&#x884C;&#x7ED3;&#x679C;"> # </a></h3>
<p>&#x5982;&#x679C;&#x51E0;&#x4E2A;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x4E4B;&#x95F4;&#x5E76;&#x6CA1;&#x6709;&#x524D;&#x540E;&#x987A;&#x5E8F;&#x4E4B;&#x5206;,&#x4F46;&#x9700;&#x8981;&#x7B49;&#x591A;&#x4E2A;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x90FD;&#x5B8C;&#x6210;&#x540E;&#x624D;&#x80FD;&#x6267;&#x884C;&#x540E;&#x7EED;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x65E0;&#x6CD5;&#x5B9E;&#x73B0;&#x5E76;&#x884C;&#x8282;&#x7EA6;&#x65F6;&#x95F4;</p>
<h2 id="t32. Promise">2. Promise <a href="#t32. Promise"> # </a></h2>
<p>Promise&#x672C;&#x610F;&#x662F;&#x627F;&#x8BFA;&#xFF0C;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x7684;&#x610F;&#x601D;&#x5C31;&#x662F;&#x627F;&#x8BFA;&#x6211;<strong>&#x8FC7;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;</strong>&#x4F1A;&#x7ED9;&#x4F60;&#x4E00;&#x4E2A;&#x7ED3;&#x679C;&#x3002;
&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4F1A;&#x7528;&#x5230;<strong>&#x8FC7;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;</strong>&#xFF1F;&#x7B54;&#x6848;&#x662F;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;&#x5F02;&#x6B65;&#x662F;&#x6307;&#x53EF;&#x80FD;&#x6BD4;&#x8F83;&#x957F;&#x65F6;&#x95F4;&#x624D;&#x6709;&#x7ED3;&#x679C;&#x7684;&#x624D;&#x505A;&#xFF0C;&#x4F8B;&#x5982;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x3001;&#x8BFB;&#x53D6;&#x672C;&#x5730;&#x6587;&#x4EF6;&#x7B49;
<img src="http://7xjf2l.com1.z0.glb.clouddn.com/zhufengpromise.png" class="img-responsive"></p>
<h2 id="t43. Promise&#x7684;&#x4E09;&#x79CD;&#x72B6;&#x6001;">3. Promise&#x7684;&#x4E09;&#x79CD;&#x72B6;&#x6001; <a href="#t43. Promise&#x7684;&#x4E09;&#x79CD;&#x72B6;&#x6001;"> # </a></h2>
<ul>
<li>Pending Promise&#x5BF9;&#x8C61;&#x5B9E;&#x4F8B;&#x521B;&#x5EFA;&#x65F6;&#x5019;&#x7684;&#x521D;&#x59CB;&#x72B6;&#x6001;</li>
<li>Fulfilled &#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x6210;&#x529F;&#x7684;&#x72B6;&#x6001;</li>
<li>Rejected &#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x5931;&#x8D25;&#x7684;&#x72B6;&#x6001;</li>
</ul>
<blockquote>
<p>then &#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x7528;&#x6765;&#x6307;&#x5B9A;Promise &#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x65F6;&#x786E;&#x5B9A;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;resolve &#x65F6;&#x6267;&#x884C;&#x7B2C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF08;onFulfilled&#xFF09;&#xFF0C;reject &#x65F6;&#x6267;&#x884C;&#x7B2C;&#x4E8C;&#x4E2A;&#x51FD;&#x6570;&#xFF08;onRejected&#xFF09;</p>
</blockquote>
<h2 id="t54. &#x6784;&#x9020;&#x4E00;&#x4E2A;Promise">4. &#x6784;&#x9020;&#x4E00;&#x4E2A;Promise <a href="#t54. &#x6784;&#x9020;&#x4E00;&#x4E2A;Promise"> # </a></h2>
<h3 id="t64.1  &#x4F7F;&#x7528;Promise">4.1  &#x4F7F;&#x7528;Promise <a href="#t64.1  &#x4F7F;&#x7528;Promise"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> promise = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {
    setTimeout(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">if</span>(<span class="hljs-built_in">Math</span>.random()&gt;<span class="hljs-number">0.5</span>)
            resolve(<span class="hljs-string">&apos;This is resolve!&apos;</span>);
        <span class="hljs-keyword">else</span>
            reject(<span class="hljs-string">&apos;This is reject!&apos;</span>);
    }, <span class="hljs-number">1000</span>);
});
promise.then(Fulfilled,Rejected)
</code></pre>
<ul>
<li>&#x6784;&#x9020;&#x4E00;&#x4E2A;Promise&#x5B9E;&#x4F8B;&#x9700;&#x8981;&#x7ED9;Promise&#x6784;&#x9020;&#x51FD;&#x6570;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;</li>
<li>&#x4F20;&#x5165;&#x7684;&#x51FD;&#x6570;&#x9700;&#x8981;&#x6709;&#x4E24;&#x4E2A;&#x5F62;&#x53C2;&#xFF0C;&#x4E24;&#x4E2A;&#x5F62;&#x53C2;&#x90FD;&#x662F;function&#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#x3002;<ul>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x5F62;&#x53C2;&#x8FD0;&#x884C;&#x540E;&#x4F1A;&#x8BA9;Promise&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;resolve&#x72B6;&#x6001;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x7ED9;&#x7B2C;&#x4E00;&#x4E2A;&#x5F62;&#x53C2;&#x547D;&#x540D;&#x4E3A;resolve,&#x4F7F; Promise &#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x6210;&#x6210;&#x529F;&#xFF0C;&#x540C;&#x65F6;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7528;&#x4E8E;&#x540E;&#x7EED;&#x6210;&#x529F;&#x540E;&#x7684;&#x64CD;&#x4F5C;</li>
<li>&#x7B2C;&#x4E00;&#x4E2A;&#x5F62;&#x53C2;&#x8FD0;&#x884C;&#x540E;&#x4F1A;&#x8BA9;Promise&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;reject&#x72B6;&#x6001;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x7ED9;&#x7B2C;&#x4E00;&#x4E2A;&#x5F62;&#x53C2;&#x547D;&#x540D;&#x4E3A;reject,&#x5C06; Promise &#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x4E3A;&#x5931;&#x8D25;&#xFF0C;&#x540C;&#x65F6;&#x5C06;&#x9519;&#x8BEF;&#x7684;&#x4FE1;&#x606F;&#x4F20;&#x9012;&#x5230;&#x540E;&#x7EED;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x7684;&#x64CD;&#x4F5C;</li>
</ul>
</li>
</ul>
<h3 id="t74.2  es5&#x6A21;&#x62DF;Promise">4.2  es5&#x6A21;&#x62DF;Promise <a href="#t74.2  es5&#x6A21;&#x62DF;Promise"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Promise</span>(<span class="hljs-params">fn</span>) </span>{
    fn(<span class="hljs-function">(<span class="hljs-params">data</span>)=&gt;</span> {
        <span class="hljs-keyword">this</span>.success(data);
    }, (error)=&gt; {
        <span class="hljs-keyword">this</span>.error();
    });
}

<span class="hljs-built_in">Promise</span>.prototype.resolve = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-keyword">this</span>.success(data);
}

<span class="hljs-built_in">Promise</span>.prototype.reject = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error</span>) </span>{
    <span class="hljs-keyword">this</span>.error(error);
}

<span class="hljs-built_in">Promise</span>.prototype.then = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">success, error</span>) </span>{
    <span class="hljs-keyword">this</span>.success = success;
    <span class="hljs-keyword">this</span>.error = error;
}
</code></pre>
<h3 id="t84.3  es6&#x6A21;&#x62DF;Promise">4.3  es6&#x6A21;&#x62DF;Promise <a href="#t84.3  es6&#x6A21;&#x62DF;Promise"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Promise</span> </span>{
    <span class="hljs-keyword">constructor</span>(fn) {
        fn(<span class="hljs-function">(<span class="hljs-params">data</span>)=&gt;</span> {
            <span class="hljs-keyword">this</span>.success(data);
        }, (error)=&gt; {
            <span class="hljs-keyword">this</span>.error();
        });
    }

    resolve(data) {
        <span class="hljs-keyword">this</span>.success(data);
    }

    reject(error) {
        <span class="hljs-keyword">this</span>.error(error);
    }

    then(success, error) {
        <span class="hljs-keyword">this</span>.success = success;
        <span class="hljs-keyword">this</span>.error = error;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<h2 id="t95. promise &#x505A;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;">5. promise &#x505A;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C; <a href="#t95. promise &#x505A;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;"> # </a></h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ajaxPromise</span> (<span class="hljs-params">queryUrl</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
    xhr.open(<span class="hljs-string">&apos;GET&apos;</span>, queryUrl, <span class="hljs-literal">true</span>);
    xhr.send(<span class="hljs-literal">null</span>);
    xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
      <span class="hljs-keyword">if</span> (xhr.readyState === <span class="hljs-number">4</span>) {
        <span class="hljs-keyword">if</span> (xhr.status === <span class="hljs-number">200</span>) {
          resolve(xhr.responseText);
        } <span class="hljs-keyword">else</span> {
          reject(xhr.responseText);
        }
      }
    }
  });
}

ajaxPromise(<span class="hljs-string">&apos;http://www.baidu.com&apos;</span>)
  .then(<span class="hljs-function">(<span class="hljs-params">value</span>) =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(value);
  })
  .catch(<span class="hljs-function">(<span class="hljs-params">err</span>) =&gt;</span> {
    <span class="hljs-built_in">console</span>.error(err);
  });
</code></pre>
<h2 id="t106.promise&#x7684;&#x94FE;&#x5F0F;&#x8C03;&#x7528;">6.promise&#x7684;&#x94FE;&#x5F0F;&#x8C03;&#x7528; <a href="#t106.promise&#x7684;&#x94FE;&#x5F0F;&#x8C03;&#x7528;"> # </a></h2>
<ul>
<li>&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x65B0;&#x7684;Promise&#x5B9E;&#x4F8B;</li>
<li>&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#x53C2;&#x6570;&#x901A;&#x8FC7;&#x8FD4;&#x56DE;&#x503C;&#x4F20;&#x9012;</li>
</ul>
<p><code>then</code>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#x5199;&#x6CD5;&#x539F;&#x56E0;&#x5728;&#x4E8E;&#xFF0C;&#x6BCF;&#x4E00;&#x6B21;&#x6267;&#x884C;&#x8BE5;&#x65B9;&#x6CD5;&#x65F6;&#x603B;&#x662F;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>Promise</code>&#x5BF9;&#x8C61;</p>
<pre><code class="lang-javascript">readFile(<span class="hljs-string">&apos;1.txt&apos;</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-built_in">console</span>.log(data);
    <span class="hljs-keyword">return</span> data;
}).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-built_in">console</span>.log(data);
    <span class="hljs-keyword">return</span> readFile(data);
}).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
    <span class="hljs-built_in">console</span>.log(data);
}).catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>)</span>{
 <span class="hljs-built_in">console</span>.log(err);
});
</code></pre>
<h2 id="t117.promise API">7.promise API <a href="#t117.promise API"> # </a></h2>
<h3 id="t127.1 Promise.all">7.1 Promise.all <a href="#t127.1 Promise.all"> # </a></h3>
<ul>
<li><strong>&#x53C2;&#x6570;</strong>&#xFF1A;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x6570;&#x7EC4;&#x5185;&#x90FD;&#x662F;<code>Promise</code>&#x5B9E;&#x4F8B;</li>
<li><strong>&#x8FD4;&#x56DE;&#x503C;</strong>&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>Promise</code>&#x5B9E;&#x4F8B;&#xFF0C;&#x8FD9;&#x4E2A;<code>Promise</code>&#x5B9E;&#x4F8B;&#x7684;&#x72B6;&#x6001;&#x8F6C;&#x79FB;&#x53D6;&#x51B3;&#x4E8E;&#x53C2;&#x6570;&#x7684;<code>Promise</code>&#x5B9E;&#x4F8B;&#x7684;&#x72B6;&#x6001;&#x53D8;&#x5316;&#x3002;&#x5F53;&#x53C2;&#x6570;&#x4E2D;&#x6240;&#x6709;&#x7684;&#x5B9E;&#x4F8B;&#x90FD;&#x5904;&#x4E8E;<code>resolve</code>&#x72B6;&#x6001;&#x65F6;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;<code>Promise</code>&#x5B9E;&#x4F8B;&#x4F1A;&#x53D8;&#x4E3A;<code>resolve</code>&#x72B6;&#x6001;&#x3002;&#x5982;&#x679C;&#x53C2;&#x6570;&#x4E2D;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;<code>reject</code>&#x72B6;&#x6001;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;<code>Promise</code>&#x5B9E;&#x4F8B;&#x53D8;&#x4E3A;<code>reject</code>&#x72B6;&#x6001;&#x3002;<pre><code class="lang-javascript"><span class="hljs-built_in">Promise</span>.all([p1, p2]).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">result</span>) </span>{
  <span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// [ &apos;2.txt&apos;, &apos;2&apos; ]</span>
});
</code></pre>
<blockquote>
<p> &#x4E0D;&#x7BA1;&#x4E24;&#x4E2A;promise&#x8C01;&#x5148;&#x5B8C;&#x6210;&#xFF0C;Promise.all &#x65B9;&#x6CD5;&#x4F1A;&#x6309;&#x7167;&#x6570;&#x7EC4;&#x91CC;&#x9762;&#x7684;&#x987A;&#x5E8F;&#x5C06;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;</p>
</blockquote>
</li>
</ul>
<h3 id="t137.2 Promise.race">7.2 Promise.race <a href="#t137.2 Promise.race"> # </a></h3>
<ul>
<li><strong>&#x53C2;&#x6570;</strong>&#xFF1A;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x6570;&#x7EC4;&#x5185;&#x90FD;&#x662F;<code>Promise</code>&#x5B9E;&#x4F8B;</li>
<li><strong>&#x8FD4;&#x56DE;&#x503C;</strong>&#xFF1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>Promise</code>&#x5B9E;&#x4F8B;&#xFF0C;&#x8FD9;&#x4E2A;<code>Promise</code>&#x5B9E;&#x4F8B;&#x7684;&#x72B6;&#x6001;&#x8F6C;&#x79FB;&#x53D6;&#x51B3;&#x4E8E;&#x53C2;&#x6570;&#x7684;<code>Promise</code>&#x5B9E;&#x4F8B;&#x7684;&#x72B6;&#x6001;&#x53D8;&#x5316;&#x3002;&#x5F53;&#x53C2;&#x6570;&#x4E2D;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;<code>resolve</code>&#x72B6;&#x6001;&#x65F6;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;<code>Promise</code>&#x5B9E;&#x4F8B;&#x4F1A;&#x53D8;&#x4E3A;<code>resolve</code>&#x72B6;&#x6001;&#x3002;&#x5982;&#x679C;&#x53C2;&#x6570;&#x4E2D;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;<code>reject</code>&#x72B6;&#x6001;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;<code>Promise</code>&#x5B9E;&#x4F8B;&#x53D8;&#x4E3A;<code>reject</code>&#x72B6;&#x6001;&#x3002;<pre><code class="lang-javascript"><span class="hljs-built_in">Promise</span>.race([p1, p2]).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">result</span>) </span>{
  <span class="hljs-built_in">console</span>.log(result); <span class="hljs-comment">// [ &apos;2.txt&apos;, &apos;2&apos; ]</span>
});
</code></pre>
<h3 id="t147.3 Promise.resolve">7.3 Promise.resolve <a href="#t147.3 Promise.resolve"> # </a></h3>
&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>Promise</code>&#x5B9E;&#x4F8B;&#xFF0C;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;<code>resolve</code>&#x72B6;&#x6001;&#x3002;</li>
</ul>
<p>&#x6839;&#x636E;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x4E0D;&#x540C;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x529F;&#x80FD;&#xFF1A;</p>
<ul>
<li>&#x503C;(&#x5BF9;&#x8C61;&#x3001;&#x6570;&#x7EC4;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x7B49;)&#xFF1A;&#x4F5C;&#x4E3A;<code>resolve</code>&#x4F20;&#x9012;&#x51FA;&#x53BB;&#x7684;&#x503C;</li>
<li><code>Promise</code>&#x5B9E;&#x4F8B;&#xFF1A;&#x539F;&#x5C01;&#x4E0D;&#x52A8;&#x8FD4;&#x56DE;</li>
</ul>
<h3 id="t157.4 Promise.reject">7.4 Promise.reject <a href="#t157.4 Promise.reject"> # </a></h3>
<p>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>Promise</code>&#x5B9E;&#x4F8B;&#xFF0C;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x5904;&#x4E8E;<code>reject</code>&#x72B6;&#x6001;&#x3002;</p>
<ul>
<li>&#x53C2;&#x6570;&#x4E00;&#x822C;&#x5C31;&#x662F;&#x629B;&#x51FA;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;</li>
</ul>
<h2 id="t168. q">8. q <a href="#t168. q"> # </a></h2>
<p>Q&#x662F;&#x4E00;&#x4E2A;&#x5728;Javascript&#x4E2D;&#x5B9E;&#x73B0;promise&#x7684;&#x6A21;&#x5757;</p>
<h3 id="t178.1 q&#x7684;&#x57FA;&#x672C;&#x7528;&#x6CD5;">8.1 q&#x7684;&#x57FA;&#x672C;&#x7528;&#x6CD5; <a href="#t178.1 q&#x7684;&#x57FA;&#x672C;&#x7528;&#x6CD5;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> Q = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;q&apos;</span>);
<span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span>(<span class="hljs-params">filename</span>) </span>{
    <span class="hljs-keyword">var</span> deferred = Q.defer();
    fs.readFile(filename,<span class="hljs-string">&apos;utf8&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, data</span>) </span>{
        <span class="hljs-keyword">if</span>(err){
            deferred.reject(err);
        }<span class="hljs-keyword">else</span>{
            deferred.resolve(data);
        }
    });
    <span class="hljs-keyword">return</span> deferred.promise;
}

read(<span class="hljs-string">&apos;1.txt1&apos;</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>)</span>{
    <span class="hljs-built_in">console</span>.log(data);
},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>)</span>{
    <span class="hljs-built_in">console</span>.error(error);
});
</code></pre>
<h3 id="t188.2 q&#x7684;&#x7B80;&#x5355;&#x5B9E;&#x73B0;">8.2 q&#x7684;&#x7B80;&#x5355;&#x5B9E;&#x73B0; <a href="#t188.2 q&#x7684;&#x7B80;&#x5355;&#x5B9E;&#x73B0;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
    defer(){
        <span class="hljs-keyword">var</span> _success,_error;
        <span class="hljs-keyword">return</span> {
            resolve(data){
                _success(data);
            },
            reject(err){
                _error(err);
            },
            <span class="hljs-attr">promise</span>:{
                then(success,error){
                    _success = success;
                    _error = error;
                }
            }
        }
    }
}
</code></pre>
<h3 id="t198.3 q&#x7684;&#x5B9E;&#x73B0;">8.3 q&#x7684;&#x5B9E;&#x73B0; <a href="#t198.3 q&#x7684;&#x5B9E;&#x73B0;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> defer = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> pending = [], value;
    <span class="hljs-keyword">return</span> {
        <span class="hljs-attr">resolve</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">_value</span>) </span>{
            <span class="hljs-keyword">if</span> (pending) {
                value = _value;
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, ii = pending.length; i &lt; ii; i++) {
                    <span class="hljs-keyword">var</span> callback = pending[i];
                    callback(value);
                }
                pending = <span class="hljs-literal">undefined</span>;
            }
        },
        <span class="hljs-attr">promise</span>: {
            <span class="hljs-attr">then</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">callback</span>) </span>{
                <span class="hljs-keyword">if</span> (pending) {
                    pending.push(callback);
                } <span class="hljs-keyword">else</span> {
                    callback(value);
                }
            }
        }
    };
};
</code></pre>
<h2 id="t209. bluebird">9. bluebird <a href="#t209. bluebird"> # </a></h2>
<p>&#x5B9E;&#x73B0; promise &#x6807;&#x51C6;&#x7684;&#x5E93;&#x662F;&#x529F;&#x80FD;&#x6700;&#x5168;&#xFF0C;&#x901F;&#x5EA6;&#x6700;&#x5FEB;&#x7684;&#x4E00;&#x4E2A;&#x5E93;</p>
<h3 id="t219.1  bluebird&#x7ECF;&#x5178;&#x4F7F;&#x7528;">9.1  bluebird&#x7ECF;&#x5178;&#x4F7F;&#x7528; <a href="#t219.1  bluebird&#x7ECF;&#x5178;&#x4F7F;&#x7528;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./bluebird&apos;</span>);

<span class="hljs-keyword">var</span> readFile = <span class="hljs-built_in">Promise</span>.promisify(<span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>).readFile);
readFile(<span class="hljs-string">&quot;1.txt&quot;</span>, <span class="hljs-string">&quot;utf8&quot;</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">contents</span>) </span>{
    <span class="hljs-built_in">console</span>.log(contents);
})

<span class="hljs-keyword">var</span> fs = <span class="hljs-built_in">Promise</span>.promisifyAll(<span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;fs&quot;</span>));

fs.readFileAsync(<span class="hljs-string">&quot;1.txt&quot;</span>, <span class="hljs-string">&quot;utf8&quot;</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">contents</span>) </span>{
    <span class="hljs-built_in">console</span>.log(contents);
})

</code></pre>
<h3 id="t229.2  bluebird&#x7B80;&#x5355;&#x5B9E;&#x73B0;">9.2  bluebird&#x7B80;&#x5355;&#x5B9E;&#x73B0; <a href="#t229.2  bluebird&#x7B80;&#x5355;&#x5B9E;&#x73B0;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
    promisify(fn){
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">var</span> args = <span class="hljs-built_in">Array</span>.from(<span class="hljs-built_in">arguments</span>);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve, reject</span>) </span>{
                fn.apply(<span class="hljs-literal">null</span>, args.concat(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
                    <span class="hljs-keyword">if</span> (err) {
                        reject(err);
                    } <span class="hljs-keyword">else</span> {
                        resolve(<span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>])
                    }
                }));
            })
        }
    },
    promisifyAll(obj){
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> attr <span class="hljs-keyword">in</span> obj){
            <span class="hljs-keyword">if</span>(obj.hasOwnProperty(attr) &amp;&amp; <span class="hljs-keyword">typeof</span> obj[attr] ==<span class="hljs-string">&apos;function&apos;</span>){
                obj[attr+<span class="hljs-string">&apos;Async&apos;</span>] = <span class="hljs-keyword">this</span>.promisify(obj[attr]);
            }
        }
        <span class="hljs-keyword">return</span> obj;
    }
}
</code></pre>
<h2 id="t2310. &#x52A8;&#x753B;">10. &#x52A8;&#x753B; <a href="#t2310. &#x52A8;&#x753B;"> # </a></h2>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>move<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-class">.square</span>{
            <span class="hljs-attribute">width</span>:<span class="hljs-number">40px</span>;
            <span class="hljs-attribute">height</span>:<span class="hljs-number">40px</span>;
            <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">50%</span>;
        }
        <span class="hljs-selector-class">.square1</span>{
            <span class="hljs-attribute">background-color</span>: red;
        }
        <span class="hljs-selector-class">.square2</span>{
            <span class="hljs-attribute">background-color</span>: yellow;
        }
        <span class="hljs-selector-class">.square3</span>{
            <span class="hljs-attribute">background-color</span>: blue;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;square square1&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;margin-left: 0&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;square square2&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;margin-left: 0&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;square square3&quot;</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;margin-left: 0&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">var</span> square1 = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;.square1&apos;</span>);
  <span class="hljs-keyword">var</span> square2 = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;.square2&apos;</span>);
  <span class="hljs-keyword">var</span> square3 = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;.square3&apos;</span>);

  <span class="hljs-comment">/*function move(element,target,resolve){
    let timer = setInterval(function(){
      var marginLeft = parseInt(element.style.marginLeft, 10);
      if(marginLeft == target){
        resolve();
      }else{
        element.style.marginLeft = ++marginLeft+&apos;px&apos;;
      }
    },13);
  }*/</span>
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">move</span>(<span class="hljs-params">element,target,resolve</span>)</span>{
    <span class="hljs-keyword">let</span> current = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">let</span> timer = setInterval(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
      element.style.transform=<span class="hljs-string">`translateX(<span class="hljs-subst">${++current}</span>px)`</span>;
      <span class="hljs-keyword">if</span>(current&gt;target){
        clearInterval(timer);
        resolve();
      };
    },<span class="hljs-number">13</span>);
  }
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">animate</span>(<span class="hljs-params">element,target</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resolve,reject</span>)</span>{
      move(element,target,resolve);
    });
  }
  animate(square1,<span class="hljs-number">100</span>)
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
      <span class="hljs-keyword">return</span> animate(square2,<span class="hljs-number">100</span>);
    })
    .then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
      <span class="hljs-keyword">return</span> animate(square3,<span class="hljs-number">100</span>);
    });
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h2 id="t2411. co">11. co <a href="#t2411. co"> # </a></h2>
<h3 id="t2511.1  co&#x521D;&#x4F53;&#x9A8C;">11.1  co&#x521D;&#x4F53;&#x9A8C; <a href="#t2511.1  co&#x521D;&#x4F53;&#x9A8C;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getNumber</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve,reject</span>) </span>{
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
      <span class="hljs-keyword">let</span> number = <span class="hljs-built_in">Math</span>.random();
      <span class="hljs-keyword">if</span>(number &gt;<span class="hljs-number">.5</span>){
        resolve(number);
      }<span class="hljs-keyword">else</span>{
        reject(<span class="hljs-string">&apos;&#x6570;&#x5B57;&#x592A;&#x5C0F;&apos;</span>);
      }
    },<span class="hljs-number">1000</span>);
  });
}
<span class="hljs-function"><span class="hljs-keyword">function</span> *<span class="hljs-title">read</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">let</span> a = <span class="hljs-keyword">yield</span> getNumber();
  <span class="hljs-built_in">console</span>.log(a);
  <span class="hljs-keyword">let</span> b = <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;b&apos;</span>;
  <span class="hljs-built_in">console</span>.log(b);
  <span class="hljs-keyword">let</span> c = <span class="hljs-keyword">yield</span> getNumber();
  <span class="hljs-built_in">console</span>.log(c);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">co</span>(<span class="hljs-params">gen</span>)</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resolve,reject</span>)</span>{
    <span class="hljs-keyword">let</span> g = gen();
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">next</span>(<span class="hljs-params">lastValue</span>)</span>{
      <span class="hljs-keyword">let</span> {done,value} = g.next(lastValue);
      <span class="hljs-keyword">if</span>(done){
         resolve(lastValue);
      }<span class="hljs-keyword">else</span>{
        <span class="hljs-keyword">if</span>(value <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Promise</span>){
          value.then(next,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val</span>)</span>{
            reject(val);
          });
        }<span class="hljs-keyword">else</span>{
          next(value);
        }
      }
    }
    next();
  });
}
co(read).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>)</span>{
  <span class="hljs-built_in">console</span>.log(data);
},<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">reason</span>)</span>{
  <span class="hljs-built_in">console</span>.log(reason);
});
</code></pre>
<h3 id="t2611.2  co&#x8FDE;&#x7EED;&#x8BFB;&#x6587;&#x4EF6;">11.2  co&#x8FDE;&#x7EED;&#x8BFB;&#x6587;&#x4EF6; <a href="#t2611.2  co&#x8FDE;&#x7EED;&#x8BFB;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">readFile</span>(<span class="hljs-params">filename</span>)</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve,reject</span>) </span>{
    fs.readFile(filename,<span class="hljs-string">&apos;utf8&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,data</span>)</span>{
      <span class="hljs-keyword">if</span>(err)
        reject(err);
      <span class="hljs-keyword">else</span>
        resolve(data);
    })
  });
}
<span class="hljs-function"><span class="hljs-keyword">function</span> *<span class="hljs-title">read</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">let</span> a = <span class="hljs-keyword">yield</span> readFile(<span class="hljs-string">&apos;./1.txt&apos;</span>);
  <span class="hljs-built_in">console</span>.log(a);
  <span class="hljs-keyword">let</span> b = <span class="hljs-keyword">yield</span> readFile(<span class="hljs-string">&apos;./2.txt&apos;</span>);
  <span class="hljs-built_in">console</span>.log(b);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">co</span>(<span class="hljs-params">gen</span>)</span>{
  <span class="hljs-keyword">let</span> g = gen();
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">next</span>(<span class="hljs-params">val</span>)</span>{
    <span class="hljs-keyword">let</span> {done,value} = g.next(val);
    <span class="hljs-keyword">if</span>(!done){
      value.then(next);
    }
  }
  next();
}
</code></pre>
<h2 id="t2712. Promise/A+&#x5B8C;&#x6574;&#x5B9E;&#x73B0;">12. Promise/A+&#x5B8C;&#x6574;&#x5B9E;&#x73B0; <a href="#t2712. Promise/A+&#x5B8C;&#x6574;&#x5B9E;&#x73B0;"> # </a></h2>
<pre><code>function Promise(executor) {
  let self = this;
  self.status = &quot;pending&quot;;
  self.value = undefined;
  self.onResolvedCallbacks = [];
  self.onRejectedCallbacks = [];
  function resolve(value) {
    if (value instanceof Promise) {
      return value.then(resolve, reject)
    }
    setTimeout(function () { // &#x5F02;&#x6B65;&#x6267;&#x884C;&#x6240;&#x6709;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;
      if (self.status == &apos;pending&apos;) {
        self.value = value;
        self.status = &apos;resolved&apos;;
        self.onResolvedCallbacks.forEach(item =&gt; item(value));
      }
    });

  }

  function reject(value) {
    setTimeout(function () {
      if (self.status == &apos;pending&apos;) {
        self.value = value;
        self.status = &apos;rejected&apos;;
        self.onRejectedCallbacks.forEach(item =&gt; item(value));
      }
    });
  }

  try {
    executor(resolve, reject);
  } catch (e) {
    reject(e);
  }
}
function resolvePromise(promise2, x, resolve, reject) {
  if (promise2 === x) {
    return reject(new TypeError(&apos;&#x5FAA;&#x73AF;&#x5F15;&#x7528;&apos;));
  }
  let then, called;

  if (x != null &amp;&amp; ((typeof x == &apos;object&apos; || typeof x == &apos;function&apos;))) {
    try {
      then = x.then;
      if (typeof then == &apos;function&apos;) {
        then.call(x, function (y) {
          if (called)return;
          called = true;
          resolvePromise(promise2, y, resolve, reject);
        }, function (r) {
          if (called)return;
          called = true;
          reject(r);
        });
      } else {
        resolve(x);
      }
    } catch (e) {
      if (called)return;
      called = true;
      reject(e);
    }
  } else {
    resolve(x);
  }
}
Promise.prototype.then = function (onFulfilled, onRejected) {
  let self = this;
  onFulfilled = typeof onFulfilled == &apos;function&apos; ? onFulfilled : function (value) {
    return value
  };
  onRejected = typeof onRejected == &apos;function&apos; ? onRejected : function (value) {
    throw value
  };
  let promise2;
  if (self.status == &apos;resolved&apos;) {
    promise2 = new Promise(function (resolve, reject) {
      setTimeout(function () {
        try {
          let x = onFulfilled(self.value);
          resolvePromise(promise2, x, resolve, reject);
        } catch (e) {
          reject(e);
        }
      });

    });
  }
  if (self.status == &apos;rejected&apos;) {
    promise2 = new Promise(function (resolve, reject) {
      setTimeout(function () {
        try {
          let x = onRejected(self.value);
          resolvePromise(promise2, x, resolve, reject);
        } catch (e) {
          reject(e);
        }
      });
    });
  }
  if (self.status == &apos;pending&apos;) {
    promise2 = new Promise(function (resolve, reject) {
      self.onResolvedCallbacks.push(function (value) {
        try {
          let x = onFulfilled(value);
          resolvePromise(promise2, x, resolve, reject);
        } catch (e) {
          reject(e);
        }
      });
      self.onRejectedCallbacks.push(function (value) {
        try {
          let x = onRejected(value);
          resolvePromise(promise2, x, resolve, reject);
        } catch (e) {
          reject(e);
        }
      });
    });
  }
  return promise2;
}
Promise.prototype.catch = function (onRejected) {
  return this.then(null, onRejected);
}
Promise.all = function (promises) {
  return new Promise(function (resolve, reject) {
    let result = [];
    let count = 0;
    for (let i = 0; i &lt; promises.length; i++) {
      promises[i].then(function (data) {
        result[i] = data;
        if (++count == promises.length) {
          resolve(result);
        }
      }, function (err) {
        reject(err);
      });
    }
  });
}

Promise.deferred = Promise.defer = function () {
  var defer = {};
  defer.promise = new Promise(function (resolve, reject) {
    defer.resolve = resolve;
    defer.reject = reject;
  })
  return defer;
}
/**
 * npm i -g promises-aplus-tests
 * promises-aplus-tests Promise.js
 */
try {
  module.exports = Promise
} catch (e) {
}

</code></pre><h2 id="t2813. &#x8D44;&#x6E90;">13. &#x8D44;&#x6E90; <a href="#t2813. &#x8D44;&#x6E90;"> # </a></h2>
<ul>
<li><a href="http://bluebirdjs.com/docs/api-reference.html">bluebirdAPI</a></li>
<li><a href="http://liubin.github.io/promises-book/">JavaScript Promise&#x8FF7;&#x4F60;&#x4E66;(&#x4E2D;&#x6587;&#x7248;)</a></li>
<li><a href="http://segmentfault.com/a/1190000002452115">Promise/A+&#x89C4;&#x8303;</a></li>
<li><a href="https://promisesaplus.com/">Promise/A+</a><h2 id="t2914.&#x4F5C;&#x4E1A;">14.&#x4F5C;&#x4E1A; <a href="#t2914.&#x4F5C;&#x4E1A;"> # </a></h2>
</li>
</ul>
<p>1.&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;promise&#x7684;all&#x3001;race&#x3001;resolve&#x548C;reject&#x65B9;&#x6CD5;</p>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
